package cn.shujuhai.meeting.service.impl;

import cn.shujuhai.common.pluging.data.ResultCode;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.meeting.domain.MeetEquipment;
import cn.shujuhai.meeting.service.MeetEquipmentService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.shujuhai.meeting.domain.Equipment;
import cn.shujuhai.meeting.service.EquipmentService;
import cn.shujuhai.meeting.mapper.EquipmentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 4g0
 * @description 针对表【equipment】的数据库操作Service实现
 * @createDate 2025-06-16 10:25:17
 */
@Service
@RequiredArgsConstructor
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment>
        implements EquipmentService {
    final MeetEquipmentService meetEquipmentService;
    private static final List<Equipment> repeatList = new ArrayList<>();

    /**
     * 分页查询会议室设备信息
     *
     * @param equipmentName 设备名称（可为null，不为null时进行模糊查询）
     * @param pageNum       当前页码（从1开始）
     * @param pageSize      每页显示的记录数
     * @return 返回包含分页信息和系统设备列表的ResultVo对象，其中PageInfo<Equipment>包含了分页的详细信息，如总记录数、总页数等
     */
    @Override
    public ResultVo<PageInfo<Equipment>> getEquipmentPageInfo(String equipmentName, Integer pageNum, Integer pageSize, Long roomId) {

        PageHelper.startPage(pageNum, pageSize);
        var wrapper = getEquipmentListByRoomId(roomId);
        if (equipmentName != null && !equipmentName.isEmpty()) {
            wrapper.like("equipment_name", equipmentName);
        }
        List<Equipment> recipientList = this.list(wrapper.orderByDesc(Equipment::getEquipmentId));  // 根据用户Id降序排序
        PageInfo<Equipment> recipientPageInfo = new PageInfo<>(recipientList);
        return ResultVo.success(recipientPageInfo);
    }

    /**
     * 新增设备对象
     *
     * @param equipment 设备对象
     * @return 新增成功或失败的响应状态
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<?> add(Equipment equipment) {
        if (!this.save(equipment)) {
            return ResultVo.failure(ResultCode.FAILED);
        }
        MeetEquipment meetEquipment = new MeetEquipment();
        meetEquipment.setRoomId(equipment.getRoomId());
        meetEquipment.setEquipmentId(equipment.getEquipmentId());
        if (!this.meetEquipmentService.save(meetEquipment)) {
            return ResultVo.failure(ResultCode.FAILED);
        }
        return ResultVo.success();
    }

    /**
     * 设备编辑
     *
     * @param equipment 设备对象
     * @return 更新的设备对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<Equipment> edit(Equipment equipment) {
        if (!this.updateById(equipment)) {
            return ResultVo.failure(ResultCode.FAILED);
        }
        return ResultVo.success();
    }

    /**
     * 设备删除
     *
     * @param equipmentId 设备Id
     * @return 删除成功或失败的结果及响应信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<Equipment> removeId(Integer equipmentId) {
        this.meetEquipmentService.remove(Wrappers.<MeetEquipment>lambdaQuery().eq(MeetEquipment::getEquipmentId, equipmentId));
        if (!this.removeById(equipmentId)) {
            return ResultVo.failure(ResultCode.FAILED);
        }
        return ResultVo.success();
    }

    /**
     * 根据会议室Id查询设备列表
     *
     * @param roomId 会议室Id
     * @return 设备列表
     */
    @Override
    public ResultVo<?> equipmentListByRoomId(Long roomId) {
        List<Equipment> equipmentList = this.list(getEquipmentListByRoomId(roomId));
        return ResultVo.success(equipmentList);
    }

    /**
     * 根据会议室Id联表查询设备列表的wrapper
     *
     * @param roomId 会议室Id
     * @return 联表查询的wrapper
     */
    @Override
    public MPJLambdaWrapper<Equipment> getEquipmentListByRoomId(Long roomId) {
        var wrapper = new MPJLambdaWrapper<Equipment>();
        wrapper.select(Equipment::getEquipmentId, Equipment::getEquipmentName, Equipment::getEquipmentType, Equipment::getDescription)
                .leftJoin(MeetEquipment.class, MeetEquipment::getEquipmentId, Equipment::getEquipmentId)
                .eq(MeetEquipment::getRoomId, roomId);
        return wrapper;
    }
}




